//
//  YTFTabbarViewController.swift
//  sinaWeibo
//
//  Created by pro on 16/2/25.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit

class YTFTabbarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //创建一个自定义的tabbar,不要与系统的重名
        let tabbar = YTFTabbar()
        
        //因为tabbar的属性是只读的，不能对其直接赋值，所以使用KVC来赋值
        setValue(tabbar, forKey: "tabBar")
        
        //给按钮添加点击事件
        tabbar.composeButton.addTarget(self, action: "showPublish", forControlEvents: .TouchUpInside)
        
        addChildViewController()
        
        
        
    }
    
    
    //点击跳转到发布微博界面
    func showPublish(){
        print("我被点击了，好害羞，⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄")
        
        //实例化一个微博控制器
        let publishVC = YTFPublishViewController()
        
        let navVC = UINavigationController(rootViewController: publishVC)
        
        //跳转控制器
        presentViewController(navVC, animated: true) { () -> Void in
            
        }
        
    }
    
    
    func addChildViewController() {
        //使用抽取的方法添加自控制器
        addChildViewController(YTFHomeViewController(), image: "tabbar_home", title: "首页")
        addChildViewController(YTFMessageViewController(), image: "tabbar_message_center", title: "消息")
        addChildViewController(YTFFindViewController(), image: "tabbar_discover", title: "发现")
        addChildViewController(YTFSettingViewController(), image: "tabbar_profile", title: "设置")
    }

    
    //抽取添加子控制器的方法
    func addChildViewController(childController: UIViewController,image: String,title: String) {
        
        //childController已经给他分配了内存空间，初始化了
        
        //设置childController
        childController.tabBarItem.image = UIImage(named: image)
        //拼接选中的字符串
        let selectedImage = "\(image)_highlighted"
        childController.tabBarItem.selectedImage = UIImage(named: selectedImage)
        
        //设置title
        childController.title = title
        
                
        //把这个控制器添加到UITabBarController中
        addChildViewController(UINavigationController(rootViewController: childController))

        
    }
    

}
